package com.dxy.df.modules.controller;

import com.dxy.df.core.assit.SearchHandler;
import com.dxy.df.core.model.PageRs;
import com.dxy.df.core.model.Rs;
import com.dxy.df.modules.bean.entity.UserAccountEntity;
import com.dxy.df.modules.bean.put.UserAccountPut;
import com.dxy.df.modules.bean.query.UserAccountQuery;
import com.dxy.df.modules.bean.vo.UserAccountVo;
import com.dxy.df.modules.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户账号表
 *
 * @author biz@dxy.cn
 * @date 2019-10-25 17:27:09
 */
@Api(tags = "用户账号表")
@RestController
@RequestMapping("/api/users/account")
public class UserAccountController {
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private SearchHandler searchHandler;

    @ApiOperation(value = "用户账号表分页列表")
    @PostMapping("/search")
    public PageRs<UserAccountVo> search(@RequestBody @Valid UserAccountQuery query) {
        //查询列表数据
        List<UserAccountVo> voList = userAccountService.queryList(query);
        int total = userAccountService.queryTotal(query);
        return searchHandler.search(voList, total, query);
    }


    @ApiOperation(value = "用户账号表信息")
    @GetMapping("/info/{id}")
    public Rs<UserAccountEntity> info(@PathVariable("id") Integer id) {
        UserAccountQuery query = new UserAccountQuery();
        query.setId(id);
        UserAccountEntity userAccount = userAccountService.queryObject(query);
        return Rs.ok().putData(userAccount);
    }

    @ApiOperation(value = "用户账号表新增")
    @PostMapping
    public Rs save(@RequestBody UserAccountPut userAccount) {
        userAccountService.save(userAccount);
        return Rs.ok();
    }

    @ApiOperation(value = "用户账号表修改")
    @PutMapping
    public Rs update(@RequestBody UserAccountPut userAccount) {
        userAccountService.update(userAccount);
        return Rs.ok();
    }

    @ApiOperation(value = "用户账号表删除")
    @DeleteMapping
    public Rs delete(@RequestParam Integer[] ids) {
        userAccountService.deleteBatch(ids);
        return Rs.ok();
    }

}
